package com.shiguo365.market.display.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.R;
import com.shiguo365.market.display.activity.AddressListActivity;
import com.shiguo365.market.display.activity.OrderListActivity;
import com.shiguo365.market.display.activity.SettingActivity;
import com.shiguo365.market.display.activity.SuggestionsActivity;
import com.shiguo365.market.display.util.RefreshUtils;
import com.shiguo365.market.display.view.IUserCenterView;
import com.shiguo365.market.presenter.contract.IUserCenterPresenter;
import com.shiguo365.market.presenter.implement.UserCenterPresenter;
import com.shiguo365.market.utils.HandlerUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by caomenglong on 16/7/7.
 */
public class CenterFragment extends Fragment implements IUserCenterView, SwipeRefreshLayout.OnRefreshListener {

    private static final String TAG = "CenterFragment";

    @BindView(R.id.user_center_refresh_layout)
    protected SwipeRefreshLayout refreshLayout;

    @BindView(R.id.user_detail_tv_name)
    protected TextView mTvName;

    @BindView(R.id.user_detail_tv_phone)
    protected TextView mTvPhone;

    @BindView(R.id.user_detail_tv_balance)
    protected TextView mTvBalance;

    @BindView(R.id.user_detail_tv_orders)
    protected TextView mTvOrders;

    private Integer UID = 1;

    private IUserCenterPresenter mPresenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_center, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);
        mPresenter = new UserCenterPresenter(this.getActivity(),this);
        RefreshUtils.initOnCreate(refreshLayout, this);
        RefreshUtils.refreshOnCreate(refreshLayout, this);
        SharedPreferences preferences=this.getActivity().getSharedPreferences("data", Context.MODE_PRIVATE);
        UID=preferences.getInt("uid",1);
        Log.d(TAG,UID.toString());

    }

    @Override
    public void onRefresh() {


        mPresenter.getUserInfoById(UID);
    }


    @Override
    public boolean onGetUserInfoOk(@NonNull JSONObject userinfo) {
        Log.e(TAG,userinfo.toJSONString());
        mTvBalance.setText(userinfo.getString("amount"));
        mTvName.setText(userinfo.getString("shopName"));
        mTvPhone.setText(userinfo.getString("phone"));
        mTvOrders.setText(userinfo.getString("orderCount"));
        return true;
    }

    @Override
    public void onGetUserInfoFinished() {
        refreshLayout.setRefreshing(false);
    }


    private OtherItemAction mOrderListAction = new OtherItemAction(OrderListActivity.class);
    private OtherItemAction mAddressListAction = new OtherItemAction(AddressListActivity.class);
    private OtherItemAction mSuggestionsAction = new OtherItemAction(SuggestionsActivity.class);
    private OtherItemAction mSettingAction = new OtherItemAction(SettingActivity.class);

    @OnClick(R.id.user_detail_item_btn_order)
    protected void onBtnOrderClick(View view){
        mOrderListAction.startDelayed();
    }

    @OnClick(R.id.user_detail_item_btn_address)
    protected void onBtnAddressClick(View view){
        mAddressListAction.startDelayed();
    }

    @OnClick(R.id.user_detail_item_btn_suggestions)
    protected void onBtnSuggestionsClick(View view){
        mSuggestionsAction.startDelayed();
    }

    @OnClick(R.id.user_detail_item_btn_setting)
    protected void onBtnSetting(View view){
        mSettingAction.startDelayed();
    }



    private class OtherItemAction implements Runnable {

        private Class gotoClz;

        protected OtherItemAction(Class gotoClz) {
            this.gotoClz = gotoClz;
        }

        @Override
        public void run() {
            startActivity(new Intent(getActivity(), gotoClz));

        }

        public void startDelayed() {
            HandlerUtils.postDelayed(this, 100);
        }

    }


}
